/**
 * ajax分页插件
 * 需要在要页面中加入<div id="page" class=""></div>
 * @param {Object} thiz		click事件的主体
 * @param {Object} form		查询的form
 * @param {Object} result	结果的table
 * @param {Object} url		ajax访问的地址
 * @param {Object} template	把页面的元素替换成结果的模板
 * @param {Object} options	分页的options,详见https://github.com/lyonlai/bootstrap-paginator
 * @return {TypeName} 
 */
var loading='<span>查询中...<img src="http://imagescrm.zhenai.com/edition20130706/images/loading.gif"/></span>';
var defaultPageOptions={
		size:'normal',
		alignment:'right',
		itemContainerClass: function (type, page, current) {
             return (page === current) ? "active" : "pointer-cursor";
        },
        numberOfPages:5,
        tooltipTitles: function (type, page, current) {
                 switch (type) {
                 case "first":
                     return "第一页";
                 case "prev":
                     return "上一页";
                 case "next":
                     return "下一页";
                 case "last":
                     return "最后一页";
                 case "page":
                     return "第" + page + "页";
                 }
         }
	};
function ajaxPage(url,params,template,result,event,options) {
	$(loading).insertAfter(result);
	result.hide();
	var pageOptions=$.extend(defaultPageOptions,options);
	pageOptions.onPageChanged=function(e,oldPage,newPage){
        $.isFunction(event)?event(oldPage,newPage):{};
    };
	$.ajax({
			url:url,
			method:'get',
			data:params,			
			cache: false,
			dataType:'json',
			success:function(data){
				var r=data.result;
				var html='';
				for(var i in r) {
					var t=template;
					for(var j in r[i]) {
						var d=r[i][j];
						t=t.replace('['+j+']',d);
					}
					html+=t;
				}
				var totalPages=data.totalPages;
				if(totalPages>1) {
					var currentPage=data.currentPage>totalPages?1:data.currentPage;
					pageOptions = $.extend({
						currentPage: currentPage,
						totalPages: totalPages
			        },pageOptions);
					$('#page').show().bootstrapPaginator(pageOptions);
				} else {
					$('#page').hide();
				}
				result.find('tbody').html(html);
				result.show();
				result.next('span').remove();
			}
		})
}

/**
 * 
 * @param {Object} url		地址
 * @param {Object} params	pageHolder参数
 * @param {Object} div		分页div
 * @param {Object} cur		当前页数
 * @param {Object} total	总页数
 * @param {Object} options	参数
 */
function webPage(url,params,div,cur,total,options) {
	if(total>1) {
		var pageOptions=$.extend(defaultPageOptions,options);
		pageOptions =$.extend({
			currentPage: cur>total?1:cur,
			totalPages: total,
			onPageClicked : function(e,originalEvent,type,page){
				if(cur != page) {
					location.href=url+'?pageHolder.pageIndex='+page+params;
				}
			}
		},pageOptions);
		div.bootstrapPaginator(pageOptions);
	}
}